package com.springboot.Handle;
import com.springboot.Exception.UserException;
import com.springboot.utill.resultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
import java.util.logging.Logger;

@ControllerAdvice
public class ExceptionHandle {
    private final static org.slf4j.Logger logger=LoggerFactory.getLogger(ExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)//声明要捕获的异常类是哪个（Exception这个）
    @ResponseBody
    public Map<String, Object> handle(Exception e) {
        if (e instanceof UserException) {
            UserException userException = (UserException) e;
            return resultUtil.getResultObjectData(userException.getCode(), userException.getMessage(), null);
        } else {
            logger.error("【系统异常】{}",e);
            return resultUtil.getResultObjectData("-1", "未知错误", null);
        }
        }
    }
